import sys
import os
from tornado.web import Application
import tornado.ioloop
from Server.Handler.ServerHandler import *


def source_path(relative_path):
    if getattr(sys, 'frozen', False):
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)


urls = [
    (r"/api/v1/env/(.*)/(.*)", ServerGetHandler),
    (r"/api/v1/device/(.*)", ConnectHandler),
    (r"/api/v1/session/(.*)/(.*)", ServerPostHandler),
    (r"/api/v1/app/(.*)/(.*)", ServerPostHandler),
    (r"/api/v1/element/(.*)/(.*)", ServerPostHandler),
    (r"/api/v1/find/(.*)/(.*)", ServerPostHandler),

    (r"/(.*)", tornado.web.StaticFileHandler, {
        'path': source_path("Resource"),
        'default_filename': 'index.html'
    })
]


if __name__ == "__main__":
    app = Application(urls, {'debug': True})
    app.listen(8088)
    # 创建IOLoop实例并启动
    tornado.ioloop.IOLoop.current().start()
